﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;
    using WMS6.Web.UserControls;

    public class OutReceiptList : BasePage
    {
        protected Button btnFinish;
        protected Button btnList;
        protected Button btnSearch;
        protected HtmlForm F4012;
        protected GridView gvReceipt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ReceiptList(ReceiptQueryEntity receiptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(receiptQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InboundServiceClientProxy.GetInstance().GetOutReceiptByQueryList(receiptQuery);
            base.BindData2GridView(this.gvReceipt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            if (this.gvReceipt.Rows.Count > 0)
            {
                List<string> receiptID = new List<string>();
                List<string> receiptLineID = new List<string>();
                for (int i = 0; i < this.gvReceipt.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvReceipt.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        receiptID.Add(this.gvReceipt.DataKeys[i]["RECEIPT_ID"].ToString());
                        receiptLineID.Add(this.gvReceipt.DataKeys[i]["RECEIPT_LINE_ID"].ToString());
                    }
                }
                try
                {
                    if (receiptID.Count > 0)
                    {
                        string strtemp = OutboundDetailServiceClientProxy.GetInstance().InserOrderDetailByReceipt(this.hdnOrderID.Value, receiptID, receiptLineID);
                        this.lblMsg.Text = strtemp + "保存成功!";
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = "0";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.ReceiptID = "";
            receiptQuery.PoID = "";
            receiptQuery.ExternalReceiptID = "";
            receiptQuery.OwnerID = "";
            receiptQuery.VendorID = "";
            receiptQuery.VendorName = "";
            receiptQuery.CarrierID = "";
            receiptQuery.ReceiptDate = "";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) + 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) > 0)
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) - 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void btnSearch_Click(object sernder, EventArgs e)
        {
            base.Server.Transfer("OutReceiptQuery.aspx?OwnerID=" + this.hdnOwnerID.Value + "&OrderID=" + this.hdnOrderID.Value, false);
        }

        private ReceiptQueryEntity GetReceiptQueryEntity()
        {
            return (base.PageQueryEntityViewState as ReceiptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                receiptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void gvReceipt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
            }
        }

        private void LoadData()
        {
            ReceiptQueryEntity receiptQuery = new ReceiptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    receiptQuery.ReceiptID = this.PreviousPage.ReceiptID;
                    receiptQuery.PoID = this.PreviousPage.PoID;
                    receiptQuery.ExternalReceiptID = this.PreviousPage.ExternReceiptID;
                    receiptQuery.OwnerID = this.PreviousPage.OwnerID;
                    receiptQuery.VendorID = this.PreviousPage.VendorID;
                    receiptQuery.VendorName = this.PreviousPage.VendorName;
                    receiptQuery.ReceiptDate = this.PreviousPage.ReceiptDate;
                }
                else
                {
                    receiptQuery.ReceiptID = "";
                    receiptQuery.PoID = "";
                    receiptQuery.ExternalReceiptID = "";
                    receiptQuery.OwnerID = "";
                    receiptQuery.VendorID = "";
                    receiptQuery.VendorName = "";
                    receiptQuery.ReceiptDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.hdnOwnerID.Value = base.Request.QueryString["OwnerID"];
            this.hdnOrderID.Value = base.Request.QueryString["OrderID"];
            receiptQuery.IsGetAll = false;
            receiptQuery.CurrentPage = "0";
            receiptQuery.PageSize = this.gvReceipt.PageSize.ToString();
            receiptQuery.SortField = "RECEIPT_ID";
            receiptQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvReceipt.PageSize;
            this.SetReceiptQueryEntity(receiptQuery);
            this.BindData2ReceiptList(receiptQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetReceiptQueryEntity(ReceiptQueryEntity receiptQuery)
        {
            base.PageQueryEntityViewState = receiptQuery;
        }

        public OutReceiptQuery PreviousPage
        {
            get
            {
                return (OutReceiptQuery) base.PreviousPage;
            }
        }
    }
}

